package domain.extra;

import org.uqbar.commons.model.ObservableObject;

public class Result extends ObservableObject {

	private int number;
	private Column column;
	private Row row;
	private OddEven oddEven;

	public static final String NUMBER = "number";
	public static final String COLUMN = "column";
	public static final String ROW = "row";
	public static final String ODD_EVEN = "oddEven";

	public Result(int aNumber) {
		this.number = aNumber;
		calculateOddEven(number);
		calculateRow(number);
		calculateColumn(number);
	}

	
	public void setOddEven(OddEven parity) {
		setFieldValue(ODD_EVEN, parity);
	}
	
	public void calculateOddEven(int number) {
		if (number % 2 == 0) {
			setOddEven( OddEven.PAIR );
		} else {
			setOddEven( OddEven.ODD );
			}		
	}
	
	public void setRow(Row row) {
		setFieldValue(ROW, row);
	}

	public void calculateRow(int number) {
		int row = obtainRow(number);
		switch (row) {
		case (1):
			setRow(Row.ONE);
			break;
		case (2):
			setRow(Row.TWO);
			break;

		case (3):
			setRow(Row.THREE);
			break;

		case (4):
			setRow(Row.FOUR);
			break;

		case (5):
			setRow(Row.FIVE);
			break;

		case (6):
			setRow(Row.SIX);
			break;

		case (7):
			setRow(Row.SEVEN);
			break;

		case (8):
			setRow(Row.EIGHT);
			break;

		case (9):
			setRow(Row.NINE);
			break;

		case (10):
			setRow(Row.TEN);
			break;

		case (11):
			setRow(Row.ELEVEN);
			break;

		case (12):
			setRow(Row.TWELVE);
			break;

		}
	}

	public int obtainRow(float number) {
		double result = number / 3;
		int column = 0;
		for (int i = 0; i < 13; i++) {
			if (result > i && result <= i + 1) {
				column = i + 1;
			}
		}
		return column;
	}
	
	public void setColumn(Column column) {
		setFieldValue(COLUMN, column);	
	}
	
	public void calculateColumn(int number) {
		switch (number % 3) {
		case (1):
			setColumn(Column.FIRST);			
			break;
		case (2):
			setColumn(Column.SECOND);			
			break;
		case (0):
			setColumn(Column.THIRD);			
			break;
		}
	}

	public Column getColumn() {
		return column;
	}

	public int getNumber() {
		return number;
	}

	public Row getRow() {
		return row;
	}

	public OddEven getOddEven() {
		return oddEven;
	}

	public void setNumber(int number) {
		setFieldValue(NUMBER, number);
	}
	
	@Override
	public String toString() {
		String texto = "Salio el: "+ this.getNumber() + System.getProperty("line.separator") + "La fila es: "+ this.getRow()+ "\n";
		texto = texto + "La columna es: "+ this.getColumn() + "\n"+ "La paridad es: "+ this.getOddEven(); 
		return texto;
	}

}
